/**
 * login.html
 */
var entrance;
$(function() {
	//获取入口
	entrance = getEntranceByUrlAndUrlParam(parent.location.href, "service");
	wxAppLogin();
	// 检测是否为手机浏览器
	if (isMobile) {
		// 隐藏第三方登录(包括微信)
		$("#other_line").hide();
		$("#other_login").hide();
	}
	// 回车登录
	$(document).keyup(
			function(event) {
				if (event.keyCode == 13) {
					// 短信
					if ($("#message_layer")[0].style.display == "block"
							&& $("#message_login_click").hasClass("active")) {
						userMessageLogin();
						return;
					}
					// 用户名密码
					if ($("#user_layer")[0].style.display == "block"
							&& $("#user_login_click").hasClass("active")) {
						userLogin();
						return;
					}
				}
			});

	// 如果后台有传递过来地址栏 则
	$("#user_login_click").click(function() {
		if (!$(this).hasClass("active")) {
			return;
		}
		userLogin();
	});
	$(".reg_btn").click(userReg);
	$(".forget_btn").click(passwordReset);
	$("#code_img,#next").click(refreshCheckCode);
	$("#code_img_phone,#next_phone").click(refreshCheckCodePhone);
	refreshCheckCodePhone();
	
	$("#wechat_login").click(wxUserLogin);

	$("#message_login_click").click(function() {
		if (!$(this).hasClass("active")) {
			return;
		}
		userMessageLogin();
	});

	var msg = getParentUrlParam("msg");
	if (!$.isEmptyObject(msg) && $.isEmptyObject(getUrlParam("nomsg"))) {
		showUserErrMsg(msg);
		$("#user").click();
	}
	;
	sensors.track('Adreambox_Login', {
		entrance : entrance,
		page_type : '登录页'
	});
})

// 用户账户登录
function userLogin() {
	var u = $("#username_input").val();
	var p = $("#password_input").val();
	var c = $("#checkcode_text").val();
	var s = getParentUrlParam("service");
	var isOk = true;
	if (isNull(u)) {
		showUserErrMsg("请输入用户名。");
		return;
		isOk = false;
	}
	if (isNull(p)) {
		showUserErrMsg("请输入密码。");
		return;
		isOk = false;
	}
	if (isNull(c)) {
		showUserErrMsg("请输入验证码。");
		return;
		isOk = false;
	}
	if (!isOk) {
		return;
	}
	document.cookie = "login=" + escape(u) + ";path=/";
	var curUrl = parent.location.href.replace(/(^|\\?|&)msg=([^&]*)/, "")
			.replace(/(^|\\?|&)un=([^&]*)/, "");
	if (curUrl.indexOf("?") > -1) {
		curUrl += "&un=" + u;
	} else {
		curUrl += "?un=" + u;
	}
	// var url = casLoginApi + "?un=" + u + "&pd=" + p + "&checkcode=" + c;// +
	// "&fail=" + encodeURIComponent(curUrl);
	// url += !$.isEmptyObject(s)?("&service="+s):"";
	// parent.location.href=url;
	var url = casLoginApi;
	//url += !$.isEmptyObject(s) ? ("&service=" + s) : "";

	var body = $(parent.document.body);
	var form = $("<form method='post'></form>");
	form.attr({
		"action" : url
	});
	var input;

	input = $("<input type='hidden'>");
	input.attr({
		"name" : "username"
	});
	input.val(u);
	form.append(input);

	input = $("<input type='password'>");
	input.attr({
		"name" : "password"
	});
	input.val(p);
	form.append(input);

	input = $("<input type='hidden'>");
	input.attr({
		"name" : "checkcode"
	});
	input.val(c);
	form.append(input);
	
	if(!$.isEmptyObject(s)){
    	input = $("<input type='hidden'>");
    	input.attr({"name":"service"});
    	input.val(s);
    	form.append(input);
    }
	
	form.appendTo(body);
	form.submit();
}
// 用户注册
function userReg() {
	// var s = getParentUrlParam("service");
	var url = "./reg.html";
	// url += !$.isEmptyObject(s)?("?service="+encodeURIComponent(s)):"";
	document.location.href = url;
}

// 忘记密码
function passwordReset() {
	var url = "./reset.html";
	document.location.href = url;
}

// 刷新验证码
function refreshCheckCode() {
	$("#code_img").attr("src", '/cas/captcha.jpg?tm=' + Math.random());
}

//刷新验证码
function refreshCheckCodePhone() {
	$("#code_img_phone").attr("src", '/account/captcha.jpg?tm=' + Math.random());
}

// 如果是微信浏览器 并且不是从微信浏览器跳转过来 则使用微信登录
function wxAppLogin() {
	var ua = navigator.userAgent.toLowerCase();
	var isWeixin = ua.indexOf('micromessenger') != -1;

	if (isWeixin) {
		$("#loading").css("display", "block");
		$(".body").hide();
		var isAg = $.isEmptyObject(getParentUrlParam("client_id"));
		if (isAg) {
			//神策数据埋点
			sensors.track('Adreambox_Login', {
				entrance : entrance,
				page_type : '微信登录页'
			});
			//loginFront/getWxAppAppid
			// 跳转到微信，通过微信账号进行登录
			var s = getParentUrlParam("service");
			$.ajax({
				type : "GET",
				url : rootPath + "/loginFront/getWxAppAppid",
				contentType : "application/json;charset=UTF-8",
				success : function(data) {
					if ("0000" == data.returnCode) {
						var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+data.returnObject+"&redirect_uri="
							+ encodeURIComponent(casLoginApi
									+ "?client_id=wxapp"
									+ (!$.isEmptyObject(s) ? ("&service=" + encodeURIComponent(s))
											: ""))
							+ "&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
					parent.location.href = url;
					
					} else {
						layer.alert(data.returnMessage);
					}
				}
			});
		
		}
	}
}

// 微信扫码登录
function wxUserLogin() {
	sensors.track('Adreambox_Login', {
		entrance : entrance,
		page_type : '微信登录页'
	});
	var s = getParentUrlParam("service");
	$.ajax({
		type : "GET",
		url : rootPath + "/loginFront/getWxAppid",
		contentType : "application/json;charset=UTF-8",
		success : function(data) {
			if ("0000" == data.returnCode) {
				var url = "https://open.weixin.qq.com/connect/qrconnect?appid="+data.returnObject+"&redirect_uri="
					+ encodeURIComponent(casLoginApi
							+ "?client_id=wxuser"
							+ (!$.isEmptyObject(s) ? ("&service=" + encodeURIComponent(s))
									: ""))
					+ "&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
			parent.location.href = url;
			
			} else {
				layer.alert(data.returnMessage);
			}
		}
	});

}

// 用户短信登录
function userMessageLogin() {
	var phone = $.trim($("#phone_input").val());
	var picCode = $.trim($("#checkcode_text_phone").val());
	var checkCode = $.trim($("#message_checkcode_text").val());
	if (isNull(phone)) {
		showPhoneErrMsg("请输入手机号码");
		return;
	}
	if (isNull(picCode)) {
		showPhoneErrMsg("请输入图片验证码");
		return;
	}
	if (isNull(checkCode)) {
		showPhoneErrMsg("请输入验证码");
		return;
	}
	var s = getParentUrlParam("service");
	$.ajax({
		type : "POST",
		url : rootPath + "/userFront/loginbymsg",
		data : JSON.stringify({
			"login" : phone,
			"checkCode" : checkCode
		}),
		contentType : "application/json;charset=UTF-8",
		success : function(data) {
			if ("0000" == data.returnCode) {
				var curUrl = parent.location.href.replace(
						/(^|\\?|&)msg=([^&]*)/, "").replace(
						/(^|\\?|&)un=([^&]*)/, "");
				if (curUrl.indexOf("?") > -1) {
					curUrl += "&un=" + phone;
				} else {
					curUrl += "?un=" + phone;
				}
				var url = casLoginApi + "?un=" + phone + "&pd=" + checkCode;// +
																			// "&fail="
																			// +
																			// encodeURIComponent(curUrl);
				url += !$.isEmptyObject(s) ? ("&service=" + encodeURIComponent(s)) : "";
				parent.location.href = url;
			} else {
				showPhoneErrMsg(data.returnMessage);
			}
		}
	});
}